<template>
  <div class="hello">
    登录
    <van-form @submit="onSubmit">
  <van-field
    v-model="form.username"
    name="username"
    label="用户名"
    placeholder="用户名"
    :rules="[{ required: true, message: '请填写用户名' }]"
  />
  <van-field
    v-model="form.password"
    type="password"
    name="password"
    label="密码"
    placeholder="密码"
    :rules="[{ required: true, message: '请填写密码' }]"
  />
  <div style="margin: 16px;">
    <van-button round block type="info" native-type="submit">提交</van-button>
    
  </div>
</van-form>
<van-button @click="getdata" round block type="info" native-type="submit">明文</van-button>
  </div>
</template>

<script>
import {getRsaPublicKey,doLogin} from '@/api/index'

import { JSEncrypt } from 'jsencrypt'

export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      type:''
    }
  },
  mounted() {
   
  },
  methods: {
    onSubmit() {
      if (/^(13[0-9]|14[01,4-9]|15[0-3,5-9]|16[5-7]|17[0135678]|18[0-9]|19[189])\d{8}$/.test(this.form.username)) {
        this.type = 'LOGIN_BY_MOBILE'
      } else {
        this.type = 'LOGIN_BY_LOGIN_NAME'
      }
      getRsaPublicKey().then(res=>{
        console.log(res) 
        const jse = new JSEncrypt() // 实例化一个 jsEncrypt 对象
        jse.setPublicKey(res.data) // 配置公钥
        // let userAccount = jse.encrypt(this.loginForm.user) //加密账号
        const passWord = jse.encrypt(this.form.password) // 加密密码
        const params = {
          loginName: this.form.username,
          password: passWord,
          domain: '_4A',
          subDomain: '_4A',
          staffLoginNameTypeName: this.type
        }
        
        doLogin(params).then(res => {
          localStorage.setItem('tokenValue', res.data.tokenValue)
          localStorage.setItem('initialValue', res.data.tokenValue)
          localStorage.setItem('initialStaffId', res.data.staffId)
          console.log('res', res);
          this.$router.push({
            path: '/',
          })
        })
      })
    },
    getdata(){
      console.log('[ 1111 ] >', 123123132)
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
